////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Project Name: ARS Logs Reader
// Version: 1.0
// File Name: 'settings.cpp'
// Purpose: 'Settings' class implementation
//
////////////////////////////////////////////////////////////////////////////////////////////////////

///////// Include files ////////////////////////////////////////////////////////////////////////////

#include <QFile>
#include <QMessageBox>
#include <QColorDialog>
#include <QTextStream>
#include <QStringList>
#include <QVariant>
#include <dynamicmodel.h>

#include "dynamicviewsettings.h"

QString g_Settings_skin_dir = "./skin";
ItemStyleSettings g_Settings;

TextStyle::TextStyle()
    : QObject()
{
    m_parent = 0;
}

TextStyle::TextStyle( const QDomElement & element, TextStyle * parent )
    : QObject(parent)
{
    m_parent = parent;
    QString attrStr;

    if(element.isNull())
        return;

    attrStr = element.attribute("fgColor");
    m_fgColor = (!attrStr.isEmpty()) ? attrStr.toUInt(0, 16) : QColor();
    m_fontFamily = element.attribute("fontName");
    attrStr = element.attribute("fontSize");
    m_fontSize = (!attrStr.isEmpty()) ? attrStr.toInt() : -1;
    attrStr = element.attribute("bold");
    m_bold = static_cast<Qt::CheckState>(attrStr.toInt());
    if(m_bold != Qt::PartiallyChecked)
    {
        qDebug(qPrintable("TextStyle::TextStyle: m_bold = " + element.attribute("bold") + " " + element.attribute("nodeLabel")));
    }

    attrStr = element.attribute("italic");
    m_italic = static_cast<Qt::CheckState>(attrStr.toInt());
    if(m_italic != Qt::PartiallyChecked)
        qDebug(qPrintable("TextStyle::TextStyle: m_italic " + element.attribute("nodeLabel")));

    attrStr = element.attribute("underline");
    m_underline = static_cast<Qt::CheckState>(attrStr.toInt());
    if(m_underline != Qt::PartiallyChecked)
        qDebug(qPrintable("TextStyle::TextStyle: m_underline " + element.attribute("nodeLabel")));

    m_description = element.attribute("description");
    m_nodeLabel = element.attribute("nodeLabel");
}

QColor TextStyle::fgColor(bool native) const
{
    if(native || m_fgColor.isValid())
        return m_fgColor;

    return m_parent->fgColor();
}

void TextStyle::setFgColor( QColor color )
{
    m_fgColor = color;
}

QString TextStyle::fontFamily(bool native) const
{
    Q_UNUSED(native);

    if(m_parent)
        return g_Settings.fontFamily();

    return m_fontFamily;
}

void TextStyle::setFontFamily( const QString & family )
{
    m_fontFamily = family;
}

int TextStyle::fontSize(bool native) const
{
    Q_UNUSED(native);

    if(m_parent)
        return g_Settings.fontSize();

    return m_fontSize;
}

void TextStyle::setFontSize( int size )
{
    m_fontSize = size;
}

Qt::CheckState TextStyle::bold(bool native) const
{
    if(native || m_bold != Qt::PartiallyChecked)
        return m_bold;

    return m_parent->bold();
}

void TextStyle::setBold(Qt::CheckState bold)
{
    m_bold = bold;
}

Qt::CheckState TextStyle::italic(bool native) const
{
    if(native || m_italic != Qt::PartiallyChecked)
        return m_italic;

    return m_parent->italic();
}

void TextStyle::setItalic(Qt::CheckState italic)
{
    m_italic = italic;
}

Qt::CheckState TextStyle::underline(bool native) const
{
    if(native || m_underline != Qt::PartiallyChecked)
        return m_underline;

    return m_parent->underline();
}

void TextStyle::setUnderline(Qt::CheckState underline)
{
    m_underline = underline;
}

QFont TextStyle::font() const
{
    QFont font(fontFamily(), fontSize());
    font.setBold(bold() == Qt::Checked);
    font.setItalic(italic() == Qt::Checked);
    font.setUnderline(underline() == Qt::Checked);
    font.setStyleStrategy(QFont::PreferAntialias);
    return font;
}

QString TextStyle::description() const
{
    return m_description;
}

void TextStyle::setDescription( const QString & description )
{
    m_description = description;
}
void TextStyle::adjustPainter( QPainter * painter, const TextStyle * style, int highlightFactor )
{
//    QColor fgColor = style->fgColor();
    int newLightness;
    if(highlightFactor != 0)
    {

        QColor highlightColor = style->fgColor();

        if(highlightColor.lightness() + highlightFactor > 255)
            newLightness = 255;
        else
            newLightness = highlightColor.lightness() + highlightFactor;
//          highlightColor.setHsv(highlightColor.hue(),
//                                  highlightColor.saturation() + highlightFactor, highlightColor.value());
//      else
//          highlightColor.setHsv(255, highlightColor.saturation(), highlightColor.value());

//      painter->setPen(highlightColor);
        highlightColor.setHsl(highlightColor.hue(), highlightColor.saturation(), newLightness);
        painter->setPen(QPen(highlightColor, 5));
    }
    else
        painter->setPen(style->fgColor());

    QFont font = style->font();
    painter->setFont(font);
}

void TextStyle::toElement( QDomElement * element, QDomDocument * document )
{
    if(m_fgColor.isValid())
        element->setAttribute("fgColor", QString::number(m_fgColor.rgb(), 16));
    else
        element->setAttribute("fgColor", "");

    element->setAttribute("bold", static_cast<int>(m_bold));
    element->setAttribute("italic", static_cast<int>(m_italic));
    element->setAttribute("underline", static_cast<int>(m_underline));
    element->setAttribute("fontSize", m_fontSize);
    element->setAttribute("fontName", m_fontFamily);
    element->setAttribute("desription", m_description);
    element->setAttribute("nodeLabel", m_nodeLabel);
}

QString TextStyle::nodeLabel() const
{
    return m_nodeLabel;
}

void TextStyle::setNodeLabel( const QString & label )
{
    m_nodeLabel = label;
}

void TextStyle::operator=( const TextStyle & style )
{
    m_fgColor = style.fgColor(true);
    m_fontFamily = style.fontFamily(true);
    m_fontSize = style.fontSize(true);
    m_bold = style.bold(true);
    m_italic = style.italic(true);
    m_underline = style.underline(true);
}

///////// Class 'ExclusiveTextStyle' implementation ////////////////////////////////////////////////

ExclusiveTextStyle::ExclusiveTextStyle()
    : TextStyle()
{

}

ExclusiveTextStyle::ExclusiveTextStyle( const QDomElement & element, ItemStyle * parent )
    : TextStyle(element, parent)
{
    m_name = element.attribute("name");
}

TextStyle::Type ExclusiveTextStyle::type() const
{
    return TextStyle::TypeExclusiveStyle;
}

QString ExclusiveTextStyle::name() const
{
    return m_name;
}

void ExclusiveTextStyle::toElement( QDomElement * element, QDomDocument * document )
{
    TextStyle::toElement(element, document);
    element->setAttribute("name", m_name);
}

///////// Class 'PropertyStyleBase' implementation /////////////////////////////////////////////////

PropertyStyleBase::PropertyStyleBase()
    : TextStyle()
{

}

PropertyStyleBase::PropertyStyleBase( const QDomElement & element, TextStyle * parent )
    : TextStyle(element, parent)
{
    m_visibility = (Visibility)element.attribute("visibility").toInt();
}

PropertyStyleBase::Visibility PropertyStyleBase::visibility() const
{
    return m_visibility;
}

void PropertyStyleBase::setVisibility( PropertyStyleBase::Visibility visibility )
{
    m_visibility = visibility;
}

void PropertyStyleBase::toElement( QDomElement * element, QDomDocument * document )
{
    TextStyle::toElement(element, document);
    element->setAttribute("visibility", m_visibility);
}

void PropertyStyleBase::operator=( const PropertyStyleBase & style )
{
    TextStyle::operator=(style);
    m_visibility = style.visibility();
}

///////// Class 'DebugTreeView' implementation /////////////////////////////////////////////////////

PropertyStyle::PropertyStyle()
    : PropertyStyleBase()
{

}

PropertyStyle::PropertyStyle( const QDomElement & element, ItemStyle * parent )
    : PropertyStyleBase(element, parent)
{
    m_parent = parent;

    if(element.isNull())
        return;

    if(element.hasAttribute("type"))
        m_type = static_cast<Type>(element.attribute("type").toInt());
    else
        m_type = Undefined;

    m_isTypeSelector = element.hasAttribute("typeSelector");
    m_name = element.attribute("name");
    m_label = element.attribute("label");
    m_separator = element.attribute("separator");
    m_order = element.attribute("order").toInt();

    QDomElement child = element.firstChildElement();

    while(!child.isNull())
    {
        if(child.tagName() == "alias")
        {
            PropertyAliasStyle * style = new PropertyAliasStyle(child, this);
            m_aliasStyles.append(style);
            child = child.nextSiblingElement();
        }
    }
}

TextStyle::Type PropertyStyle::type() const
{
    return TextStyle::TypePropertyStyle;
}

ItemStyle * PropertyStyle::parentStyle()
{
    return m_parent;
}

PropertyStyle::Type PropertyStyle::type()
{
    return m_type;
}

void PropertyStyle::setType( PropertyStyle::Type type )
{
    m_type = type;
}

QString PropertyStyle::name() const
{
    return m_name;
}

void PropertyStyle::setName( const QString & name )
{
    m_name = name;
}

QString PropertyStyle::label() const
{
    return m_label;
}

void PropertyStyle::setLabel( const QString & label )
{
    m_label = label;
}

QString PropertyStyle::separator() const
{
    return m_separator;
}

void PropertyStyle::setSeparator( const QString & separator )
{
    m_separator = separator;
}

PropertyAliasStyle * PropertyStyle::styleForValue( const QVariant & value ) const
{
//    qDebug(qPrintable("PropertyStyle::styleForValue:" + QString(value.typeName()) + ":" + value.toString()));

    for(int i = 0; i < m_aliasStyles.size(); i++)
    {
        /*qDebug(qPrintable("PropertyStyle::styleForValue:alias:" +
                          QString(m_aliasStyles.at(i)->value().typeName()) + ":" +
                          m_aliasStyles.at(i)->value().toString()));*/
        if(m_aliasStyles.at(i)->value() == value)
            return m_aliasStyles.at(i);
    }

    return 0;
}

QList<PropertyAliasStyle *> PropertyStyle::aliasStyles() const
{
    return m_aliasStyles;
}

bool PropertyStyle::hasAliasStyles()
{
    return !m_aliasStyles.isEmpty();
}

bool PropertyStyle::isTypeSelector()
{
    return m_isTypeSelector;
}

void PropertyStyle::setTypeSelector( bool isSelector )
{
    m_isTypeSelector = isSelector;
}

int PropertyStyle::order()
{
    return m_order;
}

void PropertyStyle::toElement( QDomElement * element, QDomDocument * document )
{
    PropertyStyleBase::toElement(element, document);
    element->setAttribute("type", static_cast<int>(m_type));
    element->setAttribute("name", m_name);
    element->setAttribute("label", m_label);
    element->setAttribute("separator", m_separator);
    if(m_isTypeSelector)
        element->setAttribute("typeSelector", "");
    element->setAttribute("order", QString::number(m_order));

    for(int i = 0; i < m_aliasStyles.size(); i++)
    {
        QDomElement childElement = document->createElement("alias");
        m_aliasStyles.at(i)->toElement(&childElement, document);
        element->appendChild(childElement);
    }

}

///////// Class 'ARSObjectPropertyStyle' implementation ////////////////////////////////////////////

ARSObjectPropertyStyle::ARSObjectPropertyStyle( const QDomElement & element, ItemStyle * parent )
    : PropertyStyle(element, parent)
{
    m_isMultiline = element.attribute("isMultiline").toInt();
    /*if(m_isMultiline)
        qDebug(qPrintable(name() + " is multiline"));
    else
        qDebug(qPrintable(name() + " is not multiline"));*/

    QString attrStr = element.attribute("bgColor");
    m_bgColor = (!attrStr.isEmpty()) ? attrStr.toUInt(0, 16) : QColor();
}

bool ARSObjectPropertyStyle::isMultiline()
{
    return m_isMultiline;
}

QColor ARSObjectPropertyStyle::bgColor()
{
    return m_bgColor;
}

void ARSObjectPropertyStyle::toElement( QDomElement * element, QDomDocument * document )
{
    PropertyStyle::toElement(element, document);
    element->setAttribute("isMultiline", static_cast<int>(m_isMultiline));
    if(m_bgColor.isValid())
        element->setAttribute("bgColor", QString::number(m_bgColor.rgb(), 16));
    else
        element->setAttribute("bgColor", "");
}

///////// Class 'PropertyAliasStyle' implementation ////////////////////////////////////////////////

PropertyAliasStyle::PropertyAliasStyle()
    : PropertyStyleBase()
{

}

PropertyAliasStyle::PropertyAliasStyle( const QDomElement & element, PropertyStyle * parent )
    : PropertyStyleBase(element, parent)
{
    m_parent = parent;
    m_alias = element.attribute("alias");
    m_referenceType = element.attribute("referenceType");

    switch(parent->type())
    {
        case PropertyStyle::Enum:
            m_value = element.attribute("value").toInt();
            break;
        case PropertyStyle::String:
            m_value = element.attribute("value");
            break;
        case PropertyStyle::DateTime:
            /*! todo */
            break;
        case PropertyStyle::Integer:
            m_value = element.attribute("value").toInt();
            break;
        case PropertyStyle::Boolean:
            m_value = element.attribute("value").isEmpty();
            break;
        case PropertyStyle::Time:
            /*! todo */
            break;

        case PropertyStyle::Form:
            m_value = QVariant::fromValue((ARSDummyForm *)0);
            /*! todo */
            break;
        case PropertyStyle::Field:
            m_value = QVariant::fromValue((ARSDummyField *)0);
            /*! todo */
            break;
        case PropertyStyle::ActiveLink:
            m_value = QVariant::fromValue((ARSDummyActiveLink *)0);
            /*! todo */
            break;
        case PropertyStyle::Filter:
            m_value = QVariant::fromValue((ARSDummyFilter *)0);
            /*! todo */
            break;
        case PropertyStyle::Guide:
            m_value = QVariant::fromValue((ARSDummyGuide *)0);
            /*! todo */
            break;
        case PropertyStyle::ActionsList:
            /*! todo */
            break;
        case PropertyStyle::AssignPairsMap:
            /*! todo */
            break;
    }
}

TextStyle::Type PropertyAliasStyle::type() const
{
    return TextStyle::TypeAliasStyle;
}

PropertyStyle * PropertyAliasStyle::parentStyle()
{
    return m_parent;
}

QVariant PropertyAliasStyle::value()
{
    return m_value;
}

void PropertyAliasStyle::setValue( const QVariant & value )
{
    m_value = value;
}

QString PropertyAliasStyle::alias()
{
    return m_alias;
}

void PropertyAliasStyle::setAlias( const QString & alias )
{
    m_alias = alias;
}

QString PropertyAliasStyle::referenceType()
{
    return m_referenceType;
}

void PropertyAliasStyle::setReferenceType( const QString & type )
{
    m_referenceType = type;
}

void PropertyAliasStyle::toElement( QDomElement * element, QDomDocument * document )
{
    PropertyStyleBase::toElement(element, document);
    element->setAttribute("alias", m_alias);
    element->setAttribute("referenceType", m_referenceType);
    element->setAttribute("value", m_value.toString());
}

///////// Class 'ARSObjectPropertyAliasStyle' implementation ///////////////////////////////////////

ARSObjectPropertyAliasStyle::ARSObjectPropertyAliasStyle( const QDomElement & element,
                                                          PropertyStyle * parent )
    :PropertyAliasStyle(element, parent)
{
    QString attrStr = element.attribute("bgColor");
    m_bgColor = (!attrStr.isEmpty()) ? attrStr.toUInt(0, 16) : QColor();
}

QColor ARSObjectPropertyAliasStyle::bgColor()
{
    return m_bgColor;
}

void ARSObjectPropertyAliasStyle::toElement( QDomElement *element, QDomDocument * document )
{
    if(m_bgColor.isValid())
        element->setAttribute("bgColor", QString::number(m_bgColor.rgb(), 16));
    else
        element->setAttribute("bgColor", "");
}


///////// Class 'ItemStyle' implementation /////////////////////////////////////////////////////////

ItemStyle::ItemStyle()
    : TextStyle()
{

}

ItemStyle::ItemStyle( const QDomElement & element, ItemStyle * parent )
    : TextStyle(element, parent)
{
    m_parent = parent;
    m_itemType = static_cast<ItemStyle::ItemType>(element.attribute("itemType").toInt());
    m_name = element.attribute("name");
//    setObjectName(m_name);

    QDomElement child = element.firstChildElement();

    while(!child.isNull())
    {
        if(child.tagName() == "property")
        {
            PropertyStyle * style;
            if(m_itemType == ExecutionModelItem)
                style = new PropertyStyle(child, this);
            else
                style = new ARSObjectPropertyStyle(child, this);

            m_propertyStyles.append(style);
        }
        else
            if(child.tagName() == "item")
            {
                ItemStyle * childStyle = new ItemStyle(child, this);
                m_childStyles.append(childStyle);
            }
            else
                if(child.tagName() == "style")
                {
                    ExclusiveTextStyle * style = new ExclusiveTextStyle(child, this);
                    m_exclusiveStyles.append(style);
                }

        child = child.nextSiblingElement();
    }
}

TextStyle::Type ItemStyle::type() const
{
    return TextStyle::TypeItemStyle;
}

ItemStyle::ItemType ItemStyle::itemType() const
{
    return m_itemType;
}

inline PropertyStyle * ItemStyle::propertyStyle( const QString & propertyName ) const
{
    for(int i = 0; i < m_propertyStyles.size(); i++)
    {
        if(m_propertyStyles.at(i)->name() == propertyName)
            return m_propertyStyles.at(i);
    }

    if(!m_parent)
        return 0;

    return m_parent->propertyStyle(propertyName);
}

QList<PropertyStyle *> ItemStyle::propertyStyles( ItemStyle::PropertyOption option ) const
{
    PropertyStyleList properties;
    if(option == NativeOnly)
    {
        properties = m_propertyStyles;
        qSort(properties.begin(), properties.end(), propertyStyleLessThan);
        return m_propertyStyles;
    }

    const ItemStyle * style = this;
    while(style)
    {
        properties.append(style->propertyStyles());
        style = style->parentStyle();
    }

    qSort(properties.begin(), properties.end(), propertyStyleLessThan);
    return properties;
}

bool ItemStyle::propertyStyleLessThan( PropertyStyle * style1, PropertyStyle * style2 )
{
    return style1->order() < style2->order();
}

int ItemStyle::propertyStylesCount() const
{
    return m_propertyStyles.size();
}

ItemStyle * ItemStyle::parentStyle() const
{
    return m_parent;
}

int ItemStyle::childStylesCount() const
{
    return m_childStyles.size();
}

ItemStyle * ItemStyle::childStyle( int index ) const
{
    return m_childStyles.at(index);
}

QString ItemStyle::name() const
{
    return m_name;
}

int ItemStyle::exclusiveStylesCount() const
{
    return m_exclusiveStyles.size();
}

ExclusiveTextStyle * ItemStyle::exclusiveStyle( int index ) const
{
    return m_exclusiveStyles.at(index);
}

void ItemStyle::toElement( QDomElement * element, QDomDocument * document )
{
    TextStyle::toElement(element, document);

    element->setAttribute("name", m_name);
    element->setAttribute("itemType", m_itemType);
    for(int i = 0; i < m_exclusiveStyles.size(); i++)
    {
        QDomElement childElement = document->createElement("style");
        m_exclusiveStyles.at(i)->toElement(&childElement, document);
        element->appendChild(childElement);
    }

    for(int i = 0; i < m_propertyStyles.size(); i++)
    {
        QDomElement childElement = document->createElement("property");
        m_propertyStyles.at(i)->toElement(&childElement, document);
        element->appendChild(childElement);
    }

    for(int i = 0; i < m_childStyles.size(); i++)
    {
        QDomElement childElement = document->createElement("item");
        m_childStyles.at(i)->toElement(&childElement, document);
        element->appendChild(childElement);
    }
}

///////// Class 'StylePackage' implementation //////////////////////////////////////////////////////
/*
StylePackage::StylePackage(const QDomElement & element)
    : ItemStyle(element, 0)
{
    m_defaultStyle = new TextStyle(element);
}

QString StylePackage::name() const
{
    return m_name;
}

QString StylePackage::description() const
{
    return m_description;
}

TextStyle * StylePackage::defaultStyle() const
{
    return m_defaultStyle;
}

void StylePackage::setDefaultStyle(TextStyle * style)
{
    m_defaultStyle = style;
}*/

///////// Class 'ItemStyleSettings' implementation /////////////////////////////////////////////////

ItemStyleSettings::ItemStyleSettings( QObject * parent )
    : QObject(parent)
{
}

bool ItemStyleSettings::readSettings()
{
    return readStyleSettings();
}

bool ItemStyleSettings::readStyleSettings()
{
    QFile file(":/settings/style_settings");
    if(!file.open(QIODevice::ReadOnly))
        return false;

    if(!m_styleDocument.setContent(&file))
    {
        file.close();
        return false;
    }

    file.close();

    QDomElement element = m_styleDocument.firstChildElement();
    element = element.firstChildElement();
    qDeleteAll(m_topLevelItemStyles);
    m_topLevelItemStyles.clear();
    while(!element.isNull())
    {
        ItemStyle * rootItemStyle = new ItemStyle(element, 0);
        m_topLevelItemStyles.append(rootItemStyle);

        element = element.nextSiblingElement();
    }

    return true;
}

void ItemStyleSettings::saveStyleSettings()
{
    QFile file("settings/style_settings.xml");
    if(!file.open(QIODevice::WriteOnly))
    {
        QMessageBox::critical(0, "Error", "Cannot open settings file.");
        return;
    }

    QDomDocument document;
    QDomElement rootElement = document.createElement("items");
    document.appendChild(rootElement);

    for(int i = 0; i < m_topLevelItemStyles.size(); i++)
    {
        QDomElement element = document.createElement("item");
        m_topLevelItemStyles.at(i)->toElement(&element, &document);
        rootElement.appendChild(element);
    }

    file.write(document.toString(4).toAscii());
    file.close();
}

ItemStyle * ItemStyleSettings::itemStyle( const QString & className ) const
{
    for(int i = 0; i < m_topLevelItemStyles.size(); i++)
    {
        ItemStyle * style = findChildStyle(m_topLevelItemStyles.at(i), className);
        if(style)
            return style;
    }

    return 0;
}

QString ItemStyleSettings::fontFamily() const
{
    return m_topLevelItemStyles.at(0)->fontFamily();
}

int ItemStyleSettings::fontSize() const
{
    return m_topLevelItemStyles.at(0)->fontSize();
}

PropertyStyle * ItemStyleSettings::propertyStyle( const QString & className,
                                                  const QString & propertyName) const
{
    ItemStyle * tmpItemStyle = itemStyle(className);
    if(!tmpItemStyle)
        return 0;

    return tmpItemStyle->propertyStyle(propertyName);
}

QList<ItemStyle *> ItemStyleSettings::topLevelItemStyles()
{
    return m_topLevelItemStyles;
}

//QStringList ItemStyleSettings::recentFiles()
//{
//    return m_recentFiles;
//}

//void ItemStyleSettings::appendRecentFile( const QString & fileName )
//{
//    if(m_recentFiles.contains(fileName))
//        return;

//    if(m_recentFiles.size() == maxRecentFilesCount)
//        m_recentFiles.removeFirst();

//    // dfsdfsdfsdf
//    m_recentFiles.append(fileName);

//    QFile file("../settings/recent_files.xml");
//    QDomDocument document;
//    QDomElement filesRootElement = document.createElement("RecentFiles");
//    for(int i = 0; i < m_recentFiles.size(); i++)
//    {
//        QDomElement fileElement = document.createElement("file");
//        fileElement.setAttribute("name", m_recentFiles.at(i));
//        filesRootElement.appendChild(fileElement);
//    }
//    document.appendChild(filesRootElement);
//    file.open(QIODevice::WriteOnly);
//    file.write(document.toByteArray(4));
//    file.close();
////    emit recentFilesListChanged();
//}

QFont ItemStyleSettings::tableViewsFont() const
{
    QFont font("MS Reference Sans Serif", 8);
    font.setStyleStrategy(QFont::PreferAntialias);

    return font;
}

QString ItemStyleSettings::reportsCSS() const
{
    return m_reportsCSS;
}

//void ItemStyleSettings::readRecentFilesList()
//{
//    QFile file("/settings/recent_files.xml");
//    if(!file.exists())
//    {
//        m_recentFiles.clear();
//        return;
//    }

//    if(!file.open(QIODevice::ReadOnly))
//        return;

//    QDomDocument recentFilesDoc;
//    if(!recentFilesDoc.setContent(&file))
//        return;

//    QDomElement element = recentFilesDoc.firstChildElement();
//    element = element.firstChildElement();
//    while(!element.isNull())
//    {
//        m_recentFiles.append(element.attribute("name"));
//        element = element.nextSiblingElement();
//    }

//    file.close();
//    emit recentFilesListChanged();
//}

ItemStyle * ItemStyleSettings::findChildStyle( ItemStyle * style, const QString & name ) const
{
    if(style->name() == name)
        return style;

    for(int i = 0; i < style->childStylesCount(); i++)
    {
        ItemStyle * childStyle = findChildStyle(style->childStyle(i), name);
        if(childStyle)
            return childStyle;
    }

    return 0;
}
